<template>
	<view style="padding: 10px 20px;">
		<view style="display: flex;gap: 5px;">
			<text>快速创建: =></text>
			<uni-tag v-for="(tags, index) in argsData" :key="index" :inverted="true" :text="tags.name"
				:type="tags.color || 'primary'" @click="EventCreate(tags.name)" />
		</view>
		<uni-table type="selection" emptyText="暂无更多数据">
			<uni-tr v-for="(item, index) in data" :key="index">
				<uni-td>{{item.name}}</uni-td>
				<uni-td>{{item.value}}</uni-td>
				<uni-td>
					<view style="display: flex;gap: 10px;">
						<text style="color: #dd524d;font-size: small;" @click="EventDelete(item.id)">删除</text>
					</view>
				</uni-td>
			</uni-tr>
		</uni-table>
	</view>
</template>

<script>
	import psMethod from '@/common/psMethod';
	export default {
		data() {
			return {
				sConf: getApp().globalData,
				argsData: [], // 关键字列表 
			}
		},
		mounted() {
			this.loadDefKeyList()
		},
		props: {
			data: {
				type: Array,
				default: []
			},
			pid: {
				type: Number,
				default: 0
			},
			gid: {
				type: Number,
				default: 0
			}
		},
		methods: {
			loadDefData() {

			},
			EventCreate(e) {
				console.log('EventCreate', e);
				let that = this;
				uni.showModal({
					title: '创建产品标签',
					content: '是否要添加标签：[' + e + '] ?',
					success(res) {
						if (res.confirm) {
							that.loadKwargsPost(e);
						}
					}
				})
			},
			EventDelete(e) {
				let that = this;
				uni.showModal({
					title: '移除产品标签',
					content: '是否需要删除该标签?',
					success(res) {
						if (res.confirm) {
							that.loadKwargsDelete(e)
						}
					}
				})
			},
			async loadDefKeyList() {
				let host = this.sConf.host + '/api/keyAt/';
				let token = this.sConf.auth.token || '';
				let data = {
					mode: '商品标签'
				}
				let res = await psMethod.authenticatorGetSync(host, token, data)
				if (res.rescode === 200) {
					this.argsData = res.resmsg
				}
			},
			async loadKwargsPost(e) {
				let host = this.sConf.host + '/admin/cargoTags/';
				let token = this.sConf.auth.token || '';
				let data = {
					pid: this.pid,
					gid: this.gid,
					name: e
				}
				let res = await psMethod.authenticatorPostSync(host, token, data)
				if (res.rescode === 200) {
					uni.showToast({
						title: '创建成功'
					})
					this.$emit('reload')
				} else {
					uni.showToast({
						title: '创建失败'
					})
				}
			},
			async loadKwargsDelete(e) {
				let host = this.sConf.host + '/admin/cargoTags/' + e + '/';
				let token = this.sConf.auth.token || '';
				let res = await psMethod.authenticatorDeleteSync(host, token)
				if (res.rescode === 200) {
					uni.showToast({
						title: '移除成功'
					})
					this.$emit('reload')
				} else {
					uni.showToast({
						title: '移除失败'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>